<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="物料名称" prop="typeName">
        <el-input
          v-model="queryParams.typeName"
          placeholder="请输入物料名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['wms:type:add']"
        >新增</el-button>
      </el-col>
      
    <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    
    <el-table v-if="refreshTable"  v-loading="loading" :data="typeList" @selection-change="handleSelectionChange" 
      row-key="itemTypeId"
      :default-expand-all="isExpandAll"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
    <!--  <el-table-column type="selection" width="55" align="center" />-->
     
      <el-table-column label="物料类型名称" align="center" prop="typeName" />
      <el-table-column label="物料类型状态" align="center" prop="status" >
        <template #default="scope">
          <dict-tag v-if="scope.row.status==0" :style="{color:'green'}" :options="status" :value="scope.row.status"  />
          <dict-tag v-if="scope.row.status==1" :style="{color:'red'}" :options="status" :value="scope.row.status"  />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180" >
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['wms:type:edit']">修改</el-button>
          <el-button link type="danger" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['wms:type:remove']">删除</el-button>
        </template>
      </el-table-column>
      
    </el-table>
    
    <paginationstatus
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改物料类型对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="typeRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="所属类型" prop="">
          <el-tree-select
            v-model="form.parentId"
            :data="type1"
            :props="{ value: 'itemTypeId', label: 'typeName', children: 'children' }"
            value-key="itemTypeId"
           
            check-strictly
        />
        
        </el-form-item>
        <el-form-item label="类型名称" prop="typeName">
          <el-input v-model="form.typeName" placeholder="请输入类型名称" />
        </el-form-item>
        <el-form-item label="类型状态" prop="status" >
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Type">
import { listType, getType, delType, addType, updateType,treeselect } from "@/api/wms/type";
import { ref } from 'vue'

const { proxy } = getCurrentInstance();
const { status } = proxy.useDict('status');


const typeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const type1 = ref([]);
const isExpandAll = ref(false);
const refreshTable = ref(true);


const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    parentId: null,
    ancestors: null,
    typeName: null,
    orderNum: null,
    status: null,
  },
  rules: {
    typeName:[
      { required: true, message: "类型名称不能为空", trigger: "blur" }
    ]

  }
});

const { queryParams, form, rules } = toRefs(data);


/** 查询物料类型列表 */
function getList() {
  loading.value = true;
  listType(queryParams.value).then(response => {
    typeList.value = proxy.handleTree(response.rows, "itemTypeId");
    total.value = response.total;
    loading.value = false;
  });
}
/** 查询菜单下拉树结构 */
function getTreeselect() {
  type1.value = [];
  listType().then(response => {
    const itemtype = { itemTypeId: 0, typeName: "请选择所属类型", children: [] };
    itemtype.children = proxy.handleTree(response.rows, "itemTypeId");
    type1.value.push(itemtype);
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    itemTypeId: null,
    parentId: null,
    ancestors: null,
    typeName: null,
    orderNum: null,
    status: "0",
    delFlag: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("typeRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.itemTypeId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
  row.value=selection[0];
}

/** 新增按钮操作 */
function handleAdd(row) {
  reset();
 getTreeselect();
  if (row != null && row.itemTypeId) {
    form.value.parentId = row.itemTypeId;
  } else {
    form.value.parentId = 0;
  }
  open.value = true;
  title.value = "添加物料类型";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  getTreeselect();
  const _itemTypeId = row.itemTypeId || ids.value
  getType(_itemTypeId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改物料类型";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["typeRef"].validate(valid => {
    if (valid) {
      if (form.value.itemTypeId != null) {
        updateType(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addType(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _itemTypeIds = row.itemTypeId || ids.value;
  proxy.$modal.confirm('是否确认删除物料类型编号为"' + _itemTypeIds + '"的数据项？').then(function() {
    return delType(_itemTypeIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('wms/type/export', {
    ...queryParams.value
  }, `type_${new Date().getTime()}.xlsx`)
}

getList();
</script>
